package top.guinguo.searcher_02;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Query;
import org.apache.lucene.util.Version;
import org.junit.Before;
import org.junit.Test;
import top.guinguo.searcher_02.util.FileIndexUtils;
import top.guinguo.searcher_02.util.SearcherUtil;

import java.io.File;
import java.io.IOException;

/**
 * Created by guin_guo on 2016/12/15.
 */
public class TeatSearch {
    private SearcherUtil su;

    @Before
    public void init() {
        su = new SearcherUtil();
    }

    @Test
    public void searchByTerm() {
        su.searchByTerm("content", "i", 3);
    }
    @Test
    public void searchByTermRange() {
        //查询 name 从d开始，s结束
//        su.searchByTermRange("name", "d", "s", 10);
        //TermRange无法查询数字类型
        su.searchByTermRange("attach", "2", "5", 10);
    }
    @Test
    public void searchByNumeric() {
        //查询某个数字非范围
        su.searchByNumeric("attach", 2, 5, 10);
    }
    @Test
    public void searchByPrefix() {
        su.searchByPrefix("content", "m", 10);
    }
    @Test
    public void searchByWildcard() {
        //匹配.org结尾的email
        su.searchByWildcard("email", "*.org", 10);
        //匹配 c开头后面有七个字符的name
        su.searchByWildcard("name", "c???????", 10);
    }
    @Test
    public void searchByBoolean() {
        su.searchByBoolean(10);
    }
    @Test
    public void searchByPhrase() {
        su.searchByPhrase(10);
    }
    @Test
    public void searchByFuzzy() {
        su.searchByFuzzy(10);
    }
    @Test
    public void searchByQueryParse() throws ParseException {
        //1. 创建QueryParse对象
        QueryParser parser = new QueryParser(Version.LUCENE_35, "content", new StandardAnalyzer(Version.LUCENE_35));
        //改变空格的默认操作符，以下可以改成AND
        //parser.setDefaultOperator(QueryParser.Operator.AND);
        //开启第一个字符的通配符匹配，默认关闭，因为效率不高
        parser.setAllowLeadingWildcard(true);
        //搜索content中包含有like的
        Query query = parser.parse("like");
        //搜索有football或者basketball的，空格默认就是OR
        query = parser.parse("football basketball");
        //改变搜索域为name为guin_guo
        query = parser.parse("name: guin_guo");
        //同样可以使用*和?来进行通配符匹配
        query = parser.parse("name: c*");
        //通配符默认不能放在首位
        query = parser.parse("email: *@ggn.org");
        //匹配name中没有lisi，但是content中必须有basketball的，+和-要放置到域说明前面
        query = parser.parse("-name:lisi +basketball");
        //匹配一个区间1<=id<=3，注意：TO必须是大写
        query = parser.parse("id:[1 TO 3]");
        //闭区间匹配 1<id<3
        query = parser.parse("id:{1 TO 3}");
        //完全匹配i like play的
        query = parser.parse("\"i like play\"");
        //匹配i play之间有一个单词的 ~1
        query = parser.parse("\"i play\"~1");
        //模糊查询
        query = parser.parse("name:guinguo~");
        //没有办法匹配数字范围（自己扩展Parse）
        query = parser.parse("attach:[2 TO 10]");
        su.searchByQueryParse(query, 10);
    }
    @Test
    public void copyFiles() {
        File file = new File("E:\\test\\lucene\\example");
        for (File f : file.listFiles()) {
            String destFileName = FilenameUtils.getFullPath(f.getAbsolutePath())
                    + FilenameUtils.getBaseName(f.getName()) + ".ggn";
            try {
                FileUtils.copyFile(f, new File(destFileName));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    @Test
    public void indexFiles() {
        FileIndexUtils.index(true);
    }
    @Test
    public void testSearchPage01() {
        su.searchPage("java", 2, 5);
    }
    @Test
    public void testSearchPage02() {
        su.searchPageByAfter("java", 3, 5);
    }
}
